package client.view;

import java.awt.Color;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import server.model.administration.Admin;
import server.model.meeting.Announcement;
import server.model.meeting.Participant;
import server.model.meeting.User;
import server.model.remotes.IMeeting;

public class CreateAnnouncementGui extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1912082313317454835L;

	private GuiManager manager;
	private JTextArea txtAnnouncement;
	private JButton jButton1;
	private List lstGroups;
	private JButton btnAddParticipants;
	private JButton btnRemoveParticipants;
	private List lstParticipants;
	private JButton btnAddAnnouncement;

	private Iterator<User> users;
	private Iterator<Participant> participant;
	private Admin admin;

	public CreateAnnouncementGui(GuiManager manager) {
		this.manager = manager;

		setSize(585, 365);
		setLocation(0, 0);
		setVisible(true);
		setLayout(null);

		initGUI();
	}

	private void initGUI() {

		txtAnnouncement = new JTextArea();
		txtAnnouncement.setText("");
		txtAnnouncement.setBounds(27, 12, 187, 137);

		btnAddAnnouncement = new JButton();

		btnAddAnnouncement.setText("Voeg toe");
		btnAddAnnouncement.setBounds(27, 161, 187, 37);
		btnAddAnnouncement.addActionListener(new btnAddAnnouncementListener());

		lstParticipants = new List();
		lstParticipants.setMultipleMode(true);
		lstParticipants.setBounds(436, 12, 87, 137);

		btnRemoveParticipants = new JButton();
		btnRemoveParticipants.setText("<");
		btnRemoveParticipants.setBounds(358, 76, 60, 40);
		btnRemoveParticipants
				.addActionListener(new btnRemoveParticipantsListener());

		btnAddParticipants = new JButton();
		btnAddParticipants.setText(">");
		btnAddParticipants.setBounds(358, 34, 60, 37);
		btnAddParticipants.addActionListener(new btnAddParticipantsListener());

		lstGroups = new List();
		lstGroups.setMultipleMode(true);
		lstGroups.setBounds(250, 12, 96, 137);

		add(txtAnnouncement);
		add(btnAddAnnouncement);
		add(lstParticipants);
		add(btnRemoveParticipants);
		add(btnAddParticipants);
		add(lstGroups);

		repaint();
	}

	private class btnAddParticipantsListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {

			String[] selectedParticipants = lstGroups.getSelectedItems();
			boolean isUsed;

			for (int i = 0; i < selectedParticipants.length; i++) {
				isUsed = false;
				for (int j = 0; j < lstParticipants.getItems().length; j++) {
					if (selectedParticipants[i].matches(lstParticipants
							.getItems()[j])) {
						isUsed = true;
					}
				}
				if (!isUsed) {
					lstParticipants.add(selectedParticipants[i]);
				}
			}
		}
	}

	private class btnRemoveParticipantsListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			String[] participants = lstParticipants.getSelectedItems();
			for (int i = 0; i < participants.length; i++) {
				lstParticipants.remove(participants[i]);
			}
		}
	}
	/*
	 * Listener om melding toe te voegen 
	 */
	private class btnAddAnnouncementListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {

			Announcement announcement = new Announcement();
			announcement.setMessage(txtAnnouncement.getText());

			Participant participant = new Participant();
			String[] participants = lstParticipants.getSelectedItems();
			for (int i = 0; i < participants.length; i++) {

				announcement.addParticipant
					(admin.getParticipantByName(participants[i]));
			}

			try {
				manager.getMeetingService().addAnnouncement(announcement);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			manager.switchScreen("menuGui");

		}
		
	}

	public void reset() {

		try {
			lstGroups.removeAll();
			lstParticipants.removeAll();
			
			users = manager.getAdmin().getUsers().iterator();
			while (users.hasNext()) {
				User currentUser = users.next();
			}

			participant = manager.getAdmin().getParticipants().iterator();
			while (participant.hasNext()) {
				Participant currentParticipant = participant.next();
				lstGroups.add(currentParticipant.getName());
			}

		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
